package sctu.edu.java.year2016.team07.project;


import sctu.edu.java.year2016.team01.chenhong.day20160526.chapter8_2.C;
import sctu.edu.java.year2016.team07.project.admin.Admin;
import sctu.edu.java.year2016.team07.project.client.Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Huang Suixin on 2016/6/15.
 */
public class Land {
    public static void main(String[] args) {
        LandFrame landFrame = new LandFrame();
        Container con = landFrame.getContentPane();
        //
        con.setLayout(null);

        JPanel pan = new JPanel();
        pan.setOpaque(false);
        pan.setLayout(new GridLayout(3, 0));

        pan.setBounds(200, 220, 390, 160);

        JPanel namePan = new JPanel();
        namePan.setOpaque(false);
        JLabel jl1 = new JLabel(new ImageIcon("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\user.png"));         //标签
        jl1.setFont(new Font("dialog", 1, 20));        //设置标签字体，粗细，大小
        namePan.add(jl1);

        JTextField name = new JTextField(10);         //创建添加文本框
        name.setPreferredSize(new Dimension(10, 45));
        name.setFont(new Font("dialog", 1, 20));      //设置文本框中的字体
        name.setOpaque(false);
        name.setBorder(BorderFactory.createLoweredBevelBorder());       //设置边框具有凹入斜面边缘
        namePan.add(name);

        JPanel passwordPan = new JPanel();
        passwordPan.setOpaque(false);
        JLabel jl2 = new JLabel(new ImageIcon("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\password.png"));         //标签
        jl2.setFont(new Font("dialog", 1, 20));        //设置标签字体，粗细，大小
        passwordPan.add(jl2);


        JPasswordField passwordField = new JPasswordField(10);     //创建添加密码文本框
        passwordField.setEchoChar('*');
        passwordField.setPreferredSize(new Dimension(10, 45));
        passwordField.setFont(new Font("dialog", 1, 20));      //设置文本框中的字体
        passwordField.setOpaque(false);
        passwordField.setBorder(BorderFactory.createLoweredBevelBorder());       //设置边框具有凹入斜面边缘
        passwordPan.add(passwordField);

        JPanel buttonPan = new JPanel();
        buttonPan.setOpaque(false);
        JButton button = new JButton(new ImageIcon("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\land_button.png"));
        button.setContentAreaFilled(false);     //设置按钮透明
        button.setBorderPainted(false);     //取消显示边界
        button.setBounds(0, 0, 102, 51);


        final String[] user = new String[1];
        user[0] = "";
        final String[] password = new String[1];
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                user[0] = name.getText();
                password[0] = String.valueOf(passwordField.getPassword());
                if (user[0].equalsIgnoreCase("admin")) {
                    if (password[0].equals("admin")) {
                        landFrame.setExtendedState(JFrame.ICONIFIED);        //窗口最小化
                        new Admin();
                    }else{
                        passwordErrorDialog error = new passwordErrorDialog();
                        error.setBounds(landFrame.getWidth()/2,landFrame.getHeight()/2,200,150);
                        error.setVisible(true);
                    }
                } else if (password[0].equals("")){
                    landFrame.setExtendedState(JFrame.ICONIFIED);        //窗口最小化
                    new Client(user[0]);
                }else {
                    passwordErrorDialog error = new passwordErrorDialog();
                    error.setBounds(landFrame.getWidth() / 2, landFrame.getHeight() / 2, 200, 150);
                    error.setVisible(true);
                }
            }
        });
        buttonPan.add(button);
        pan.add(namePan);
        pan.add(passwordPan);
        pan.add(buttonPan);
        con.add(pan);
        landFrame.setVisible(true);

    }
    //密码错误弹窗
    static class passwordErrorDialog extends JDialog{
        public passwordErrorDialog() {
            Container con = getContentPane();
            setLayout(new BorderLayout());
            JPanel pan = new JPanel();
            setTitle("密码错误！");
            setResizable(false);        //设置窗口不可改变
            setAlwaysOnTop(true);       //设置窗口总在最前面
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            //标签
            JLabel label = new JLabel("您输入的密码错误");
            label.setFont(new Font("dialog", 1, 20));
            //按钮
            JButton ok = new JButton("确认");
            ok.setFont(new Font("dialog",1,20));
            ok.setContentAreaFilled(false);
            ok.setBorderPainted(false);
            ok.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            pan.add(label);
            pan.add(ok);
            con.add(pan);

        }
    }
}

